package com.kehong.demo.code;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief 游戏地图（世界）
 * @email 122162005@qq.com
 * @date 2018-07-06
 */
public class World extends JPanel {
    private Bird bird;  // 游戏主角
    private Ground ground;  // 游戏地板
    private Column column1; // 第一根柱头
    private Column column2; // 第二根柱头
    private int score;      // 游戏得分
    private BufferedImage background; // 游戏背景图片
    private BufferedImage startImage; // 游戏初始化界面
    private BufferedImage gameOverImage; // 游戏结束界面
    private StateEnum currentState; // 游戏状态

    public World() throws IOException {
        // 加载背景图资源
        background = ImageIO.read(getClass().getResource("../resources/bg.png"));
        startImage = ImageIO.read(getClass().getResource("../resources/start.png"));
        gameOverImage = ImageIO.read(getClass().getResource("../resources/gameover.png"));

        init();

        addListener();
    }

    /**
     * 初始化游戏对象
     */
    public void init() throws IOException {
        // 实例化管道
        column1 = new Column(320 + 150 );
        column2 = new Column(320 + 150 + 180);
        // 实例化地板对象
        ground = new Ground();
        // 实例化鸟类
        bird = new Bird();
        currentState = StateEnum.INIT;
    }

    @Override
    public void paint(Graphics g) {
        // 画背景图
        g.drawImage(background, 0, 0, null);
        // 绘制管道
        column1.paint(g);
        column2.paint(g);
        // 画地板
        ground.paint(g);
        // 绘制小鸟
        bird.paint(g);

        paintScore(g);

        if (currentState == StateEnum.INIT) {
            g.drawImage(startImage, 0, 0, null);
        }

        if (currentState == StateEnum.OVER) {
            g.drawImage(gameOverImage, 0, 0, null);
        }
    }

    /**
     * 绘制分数
     * @param g
     */
    public void paintScore(Graphics g) {
        Font font = new Font(Font.MONOSPACED, Font.BOLD, 30);
        g.setFont(font);
        g.setColor(Color.white);
        g.drawString(score + "", 30, 40);
    }

    public void addListener() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (currentState == StateEnum.OVER) {
                    try {
                        init();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } else if (currentState == StateEnum.INIT) {
                    currentState = StateEnum.START;
                } else if (currentState == StateEnum.START) {
                    bird.flappy();
                }
            }
        });
    }
    /**
     * 让其他的对象动起来
     */
    public void action() {
        while (true) {
            if (currentState == StateEnum.START) {

                column1.step();
                column2.step();
                bird.step();
            }

           if (bird.hit(ground, column1, column2)) {
                currentState = StateEnum.OVER;
           }

           if (bird.pass(column1, column2)) {
                score++;
           }

            // 地面动起来
            ground.step();
            repaint();

            try {
                Thread.sleep(18);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
